//
//  OrderDetailCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/8/7.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "OrderDetailCon.h"
#import "OrderTabCell.h"
#import "ShopDetailController.h"
@interface OrderDetailCon ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *listTableview;
@property(nonatomic,strong)UILabel *addresslab;

@end

@implementation OrderDetailCon

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeaderViewMethod];
    [self creatTableviewMethod];
}

-(void)creatHeaderViewMethod{
    NSDictionary *adddic = self.maindic;
    NSString *address = [NSString stringWithFormat:@"%@ %@\n%@：%@",adddic[@"addr_city"],adddic[@"address"],adddic[@"addr_realname"],adddic[@"phone"]];
    UILabel *addlab = [UILabel new];
    self.addresslab = addlab;
    addlab.text = address;
    addlab.numberOfLines = 0;
    addlab.font = [UIFont systemFontOfSize:17*kJLXWidthScale];
    addlab.textColor = [UIColor blackColor];
    [self.view addSubview:addlab];
    addlab.sd_layout
    .topSpaceToView(self.view, 5*kJLXWidthScale)
    .leftSpaceToView(self.view, 5*kJLXWidthScale)
    .rightSpaceToView(self.view, 5*kJLXWidthScale)
    .autoHeightRatio(0);
}

-(void)creatTableviewMethod{
    self.listTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 60*kJLXWidthScale, kScreenWidth, kScreenHeight - 120*kJLXWidthScale) style:UITableViewStylePlain];
    [self.view addSubview:self.listTableview];
    self.listTableview.sd_layout
    .topSpaceToView(self.addresslab, 5*kJLXWidthScale);
    if (kScreenHeight == 812) {
        self.listTableview.sd_layout
        .bottomSpaceToView(self.view, 34);
    }
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[OrderTabCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
    
}


#pragma mark --UITableViewDelegate,UITableViewDataSource--



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *arr = self.maindic[@"orderdetail"];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderTabCell*cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *ordarr = self.maindic[@"orderdetail"];
    NSDictionary *subdic = ordarr[indexPath.row];
    NSString *strurl = [MethodClass returnUrlSetStrWSDLURL:subdic[@"goods_pic"]];
    [cell.titimagevi sd_setImageWithURL:[NSURL URLWithString:strurl]];
    cell.titlab.text = [NSString stringWithFormat:@"%@",subdic[@"goods_name"]];
    cell.cklab.text = [NSString stringWithFormat:@"%@",subdic[@"attribute_detail_name"]];
    cell.pricelab.text = [NSString stringWithFormat:@"￥%@",subdic[@"goods_price"]];
    cell.pricelab.textColor = [UIColor redColor];
    cell.numlab.text = [NSString stringWithFormat:@"×%@",subdic[@"goods_count"]];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120*kJLXWidthScale;
}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 130*kJLXWidthScale;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    UIView *heview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 130*kJLXWidthScale)];
    UIView *linview = [UIView new];
    linview.backgroundColor = LineBackColor;
    [heview addSubview:linview];
    linview.sd_layout
    .topSpaceToView(heview, 0)
    .leftSpaceToView(heview, 0)
    .rightSpaceToView(heview, 0)
    .heightIs(5*kJLXWidthScale);
    
    CGFloat prnum = 0;
    for ( NSDictionary *shopdic in self.maindic[@"orderdetail"]) {
        prnum = [shopdic[@"goods_price"] floatValue]*[shopdic[@"goods_count"] integerValue]+prnum;
    }
    for (int i = 0; i < 3; i ++) {
        UILabel *rilab  = [UILabel new];
        rilab.textColor = [[UIColor grayColor]colorWithAlphaComponent:0.7];
        rilab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
        rilab.textAlignment = NSTextAlignmentRight;
        [heview addSubview:rilab];
        rilab.sd_layout
        .topSpaceToView(linview, 8*kJLXWidthScale +20*kJLXWidthScale*i)
        .leftSpaceToView(heview, 10*kJLXWidthScale)
        .rightSpaceToView(heview, 8*kJLXWidthScale)
        .heightIs(20*kJLXWidthScale);
        if (i == 0) {
            rilab.textColor = [UIColor orangeColor];
            rilab.text = [NSString stringWithFormat:@"合计：￥%.2f",prnum];
        }else if (i == 1){
            rilab.text = [NSString stringWithFormat:@"运费：￥%.2f",[self.maindic[@"freight_price"] floatValue]];
        }else if (i == 2){
            rilab.text = [NSString stringWithFormat:@"优惠：￥%.2f",[self.maindic[@"coupon_price"] floatValue]];
        }
    }
    
    UIView *linview01 = [UIView new];
    linview01.backgroundColor = LineBackColor;
    [heview addSubview:linview01];
    linview01.sd_layout
    .topSpaceToView(heview, 76*kJLXWidthScale)
    .leftSpaceToView(heview, 0)
    .rightSpaceToView(heview, 0)
    .heightIs(5*kJLXWidthScale);
    
    UILabel *lab = [UILabel new];
    lab.numberOfLines = 2;
    lab.textColor = [[UIColor grayColor]colorWithAlphaComponent:0.7];
    lab.text = [NSString stringWithFormat:@"订单编号：%@\n订单时间：%@",self.maindic[@"order_id"],self.maindic[@"addtime"]];
    lab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
    [heview addSubview:lab];
    lab.sd_layout
    .topSpaceToView(linview01, 8*kJLXWidthScale)
    .leftSpaceToView(heview, 8*kJLXWidthScale)
    .rightSpaceToView(heview, 8*kJLXWidthScale)
    .bottomSpaceToView(heview, 0);
    return heview;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *ordarr = self.maindic[@"orderdetail"];
    NSDictionary *dic = ordarr[indexPath.row];
    ShopDetailController *shopVC = [[ShopDetailController alloc]init];
    shopVC.navigationItem.title = dic[@"goods_name"];
    NSString *urlstr = [NSString stringWithFormat:@"/dashop/goods/app/getGoods?GOODS_ID=%@&USER_ID=%@",dic[@"goods_id"],[UserDefault objectForKey:LogUserID]];
    shopVC.urlStr = urlstr;
    NSMutableDictionary *mudic = [dic mutableCopy];
    [mudic setValue:dic[@"goods_id"] forKey:@"GOODS_ID"];
    shopVC.usdic = [mudic copy];
    shopVC.orderType = @"order";
    [self.navigationController pushViewController:shopVC animated:YES];
}


@end
